import { AccessLevel, Inject, SingletonProto } from "@eggjs/tegg";
import * as lark from '@larksuiteoapi/node-sdk';
import ClientConfig from "./ClientConfig";
import { EggLogger } from "typings/app";

/**
 * 飞书机器人客户端工具
 */
@SingletonProto({
    accessLevel: AccessLevel.PUBLIC
})
export class ClientAutoMesUtils {

    @Inject()
    logger: EggLogger

    /**
     * 消息发送
     */
    client: lark.Client;

    /**
     * 初始化
     */
    async init() {
        await this.createClient()
    }

    /**
     * 创建客户端连接
     */
    async createClient() {
        if (!this.client) {
            this.client = new lark.Client({
                ...ClientConfig.ApplicationRobot,
                appType: lark.AppType.ISV,
            })
        }
    }

    /**
     * 重试创建
     */
    async repeatCreateClient() {
        for (let i = 0; i < 3; i++) {
            if (!this.client) {
                this.createClient()
            } else {
                break;
            }
        }
    }
}